package com.itheima.controller;

import com.itheima.dto.EmpDto;
import com.itheima.pojo.Emp;
import com.itheima.pojo.MyResponseResult;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.apache.catalina.realm.MemoryRuleSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping//目前只有分页，没有待条件
    public MyResponseResult<PageBean<Emp>> getByPage(EmpDto dto){
        PageBean<Emp> pageBean = empService.getByPage(dto.getPage(), dto.getPageSize(),dto);
        return MyResponseResult.ok(pageBean);
    }
    @DeleteMapping("/{ids}")
    public MyResponseResult<Object> delByIds(@PathVariable Integer[] ids){
        // 参  调  响
        int i = empService.delByIds(ids);
        return i>=1?MyResponseResult.ok(null):MyResponseResult.error(Arrays.toString(ids)+"删除失败!");
    }

    @PostMapping
    public MyResponseResult<Object> insertEmp(@RequestBody Emp emp){
        // 参  调  响
        int i = empService.insertEmp(emp);
        return i>=1?MyResponseResult.ok(null):MyResponseResult.error("保存失败!");
    }

    @GetMapping("/{id}")
    public MyResponseResult<Emp> getById(@PathVariable Integer id){
        Emp emp = empService.getById(id);
        return MyResponseResult.ok(emp);
    }

    @PutMapping
    public MyResponseResult<Object>update(@RequestBody Emp emp){
        empService.update(emp);
        return MyResponseResult.ok(null);
    }
}
